package com.animal.dao;

import com.animal.bean.ManagerBean;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;

public class ManagerManage {
	public static boolean insert(String ID,String Password,String Name,String Gender,String PhoneNumber,String Position,String Email){
		boolean issuc=false;
		Connection con = Connect.getCon();
		Statement stat = null;
		try{
			stat = con.createStatement();
			String sql = "insert into animalfile09_archvist values('"+ID+"','"+Password+"','"+Name+"','"+Gender+"','"+PhoneNumber+"','"+Position+"','"+Email+"')";
			int count = stat.executeUpdate(sql);
			if(count>0){  //插入成功
				issuc=true;
			}
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("添加错误！");
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			System.out.println("insert数据库成功获取！");
			Connect.close2(con, stat);
		}
		return issuc;
	}
	public static boolean delete(String ID){
		boolean issuc = false;
		Connection con =  Connect.getCon();
		Statement stat =null;
		try{
			stat = con.createStatement();
			String sql = "delete from animalfile09_archvist where ID = '"+ID+"'";
			int count=stat.executeUpdate(sql);
			if(count>0)
				issuc = true;	
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("根据编号删除管理员数据错误！");
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			System.out.println("delete数据库成功获取！");
			Connect.close2(con, stat);
		}
		return issuc;
	}
	public static boolean update(String ID,String Password,String Name,String Gender,String PhoneNumber,String Email){
		boolean issuc = false;
		Connection con =  Connect.getCon();
		Statement stat =null;
		try{
			stat = con.createStatement();
			String sql = "update animalfile09_archvist set Password='"+Password+"',Name='"+Name+"',Gender='"+Gender+"',PhoneNumber='"+PhoneNumber+"',Email='"+Email+"' where ID = '"+ID+"'";
			int count=stat.executeUpdate(sql);
			if(count>0)
				issuc = true;
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("更新数据出错！");
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			System.out.println("update数据库成功获取！");
			Connect.close2(con, stat);
		}
		return issuc;

	}
	public static int updatepassword(String ID,String OldPassword,String NewPassword,String CheckPassword){  //修改密码
		int issuc;		//错误类型
		Connection con =  Connect.getCon();
		PreparedStatement pstmt=null;
		String password=null;
		ResultSet rs = null;
		String sql = null;
		try{
			sql = "select Password from animalfile09_archvist where ID=?";
			pstmt = con.prepareStatement(sql);			
	  		pstmt.setString(1,ID); 		
	  		rs = pstmt.executeQuery();
			if (rs.next()){
		  		password=rs.getString(1);
		  	}
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("数据库连接出错！");
		}
	  	if(OldPassword.equals(password)){
	  		if(NewPassword.equals(OldPassword)){
	  			System.out.println("新密码与旧密码相同!");
	  			issuc = 1;
	  			return issuc;
	  		}
		 	if(NewPassword.equals(CheckPassword)){
		 		try{
				 	sql = "update animalfile09_archvist set Password=? where ID=?";
				 	pstmt = con.prepareStatement(sql);
				 	pstmt.setString(1,NewPassword);
				 	pstmt.setString(2,ID); 	 						 	
				 	pstmt.executeUpdate();
				}catch(SQLException e){
					e.printStackTrace();
					System.out.println("密码修改失败!");
					issuc = 2;
					return issuc;
				}
		 		finally{
					System.out.println("密码修改成功!");
					Connect.close2(con, pstmt);
					issuc = 0;
				}		
			}
			else{
				System.out.println("两次输入的密码不一致!");	
				issuc=3;
				return issuc;
			}
		}
		else{
			System.out.println("旧密码不正确!");		
			issuc=4;
			return issuc;		
		}
	  	return issuc;
	}
	
	public static Collection selectManager(){
		Collection list = new ArrayList();
		Connection con =  Connect.getCon();
		Statement stat =null;
		ResultSet rs = null;
		//PreparedStatement pstmt = null;
		try{
			stat = con.createStatement();
			String sql = "select * from animalfile09_archvist";
			rs = stat.executeQuery(sql);
			
			//pstmt = con.prepareStatement(sql);
		  	//pstmt.setString(1,ID);
			//rs = pstmt.executeQuery();
				
			while(rs.next()){
				ManagerBean manager = new ManagerBean();
				manager.setID(rs.getString("ID"));
				manager.setPassword(rs.getString("Password"));
				manager.setName(rs.getString("Name"));
				manager.setGender(rs.getString("Gender"));
				manager.setPhoneNumber(rs.getString("PhoneNumber"));
				manager.setPosition(rs.getString("Position"));
				manager.setEmail(rs.getString("Email"));
				list.add(manager);
			}
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("查询信息出错！");
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			System.out.println("数据库成功获取！");
			//Connect.close3p(rs, con, pstmt);
			Connect.close3(rs, con, stat);
		}
		return list;
	}
	public static ManagerBean selectByID(String ID){
		ManagerBean manager = new ManagerBean();
		Connection con =  Connect.getCon();
		Statement stat =null;
		ResultSet rs = null;
		try{
			stat = con.createStatement();	
			String sql = "select * from animalfile09_archvist where ID = '"+ID+"'";
			rs = stat.executeQuery(sql);
			while(rs.next()){
				manager.setID(rs.getString("ID"));
				manager.setPassword(rs.getString("Password"));
				manager.setName(rs.getString("Name"));
				manager.setGender(rs.getString("Gender"));
				manager.setPhoneNumber(rs.getString("PhoneNumber"));
				manager.setPosition(rs.getString("Position"));
				manager.setEmail(rs.getString("Email"));
				
			}
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("查询信息出错！");
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			System.out.println("数据库成功获取！");
			Connect.close3(rs, con, stat);
		}
		return manager;
	}
	public static Collection selectByPosition(String Position){
		Collection list = new ArrayList();
		Connection con =  Connect.getCon();
		Statement stat =null;
		ResultSet rs = null;
		try{
			stat = con.createStatement();	
			String sql = "select * from animalfile09_archvist where Position = '" + Position+"'";
			//String sql = "select * from animalfile09_archvist where Position = "+Position;
			rs = stat.executeQuery(sql);
			while(rs.next()){
				ManagerBean manager = new ManagerBean();
				manager.setID(rs.getString("ID"));
				manager.setPassword(rs.getString("Password"));
				manager.setName(rs.getString("Name"));
				manager.setGender(rs.getString("Gender"));
				manager.setPhoneNumber(rs.getString("PhoneNumber"));
				manager.setPosition(rs.getString("Position"));
				manager.setEmail(rs.getString("Email"));
				list.add(manager);
			}
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("查询信息出错！");
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			System.out.println("数据库成功获取！");
			Connect.close3(rs, con, stat);
		}
		return list;
	}

}
